﻿using LD.BC;
using LD.项目;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.Host
{
    public partial class FrmFobAoiTestPoint : BaseFrm
    {
        Dictionary<string, int> axisData = new Dictionary<string, int>();
        public FrmFobAoiTestPoint()
        {
            InitializeComponent();
            this.Shown += FobAoiTestPointShown;
        }
        private void FobAoiTestPointShown(object sender, EventArgs e)
        {
            // 获取轴id
            var data = Appbus.PartNo.Config.AxisPoint.Where(x => x.Name == "检测点位").FirstOrDefault()?.AxisPointDic;
            if (data != null)
                foreach (var axis in data)
                {
                    axisData.Add(axis.Key, axis.Value.AxisId);
                }

            var points = JsonConvert.DeserializeObject<List<FobAoiTestPoint>>(Cache.GetCache(Appbus.PartNo.CurrentPartNo + "_" + CONST.FOBAOI_TESTPOINT)?.ToString() ?? "");
            if (points != null)
                foreach (var item in points)
                {
                    dgv_point.Rows.Add(item.Sort, item.Name, item.StartPoint_X, item.StartPoint_Y, item.StartPoint_R, item.StartPoint_Z, "起始示教", "起始定位",
                        item.EndPoint_X, item.EndPoint_Y, item.EndPoint_R, item.EndPoint_Z, "结束示教", "结束定位");
                }
        }

        private void dgv_point_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;

            if (rowIndex > -1 && e.ColumnIndex > -1)
            {
                DataGridViewRow _SelectRowPoint = this.dgv_point.SelectedRows[0];
                //string pointName = _SelectRowPoint.Cells[0].Value.ToString();

                if (dgv_point.Columns[e.ColumnIndex].HeaderText == "起始示教")// 起始示教
                {
                    _SelectRowPoint.Cells["起始X轴"].Value = BcFactory.Instance.Control.GetAxisPosition((ushort)axisData["X"]);
                    _SelectRowPoint.Cells["起始Y轴"].Value = BcFactory.Instance.Control.GetAxisPosition((ushort)axisData["Y"]);
                    _SelectRowPoint.Cells["起始R轴"].Value = BcFactory.Instance.Control.GetAxisPosition((ushort)axisData["R"]);
                    _SelectRowPoint.Cells["起始Z轴"].Value = BcFactory.Instance.Control.GetAxisPosition((ushort)axisData["Z"]);
                }
                else if (dgv_point.Columns[e.ColumnIndex].HeaderText == "起始定位")// 起始定位
                {
                    BcFactory.Instance.Control.Move_Location((ushort)axisData["X"], (double)_SelectRowPoint.Cells["起始X轴"].Value);
                    BcFactory.Instance.Control.Move_Location((ushort)axisData["Y"], (double)_SelectRowPoint.Cells["起始Y轴"].Value);
                    BcFactory.Instance.Control.Move_Location((ushort)axisData["R"], (double)_SelectRowPoint.Cells["起始R轴"].Value);
                    BcFactory.Instance.Control.Move_Location((ushort)axisData["Z"], (double)_SelectRowPoint.Cells["起始Z轴"].Value);
                }
                else if (dgv_point.Columns[e.ColumnIndex].HeaderText == "结束示教")
                {
                    _SelectRowPoint.Cells["结束X轴"].Value = BcFactory.Instance.Control.GetAxisPosition((ushort)axisData["X"]);
                    _SelectRowPoint.Cells["结束Y轴"].Value = BcFactory.Instance.Control.GetAxisPosition((ushort)axisData["Y"]);
                    _SelectRowPoint.Cells["结束R轴"].Value = BcFactory.Instance.Control.GetAxisPosition((ushort)axisData["R"]);
                    _SelectRowPoint.Cells["结束Z轴"].Value = BcFactory.Instance.Control.GetAxisPosition((ushort)axisData["Z"]);
                }
                else if (dgv_point.Columns[e.ColumnIndex].HeaderText == "结束定位")
                {
                    BcFactory.Instance.Control.Move_Location((ushort)axisData["X"], (double)_SelectRowPoint.Cells["结束X轴"].Value);
                    BcFactory.Instance.Control.Move_Location((ushort)axisData["Y"], (double)_SelectRowPoint.Cells["结束Y轴"].Value);
                    BcFactory.Instance.Control.Move_Location((ushort)axisData["R"], (double)_SelectRowPoint.Cells["结束R轴"].Value);
                    BcFactory.Instance.Control.Move_Location((ushort)axisData["Z"], (double)_SelectRowPoint.Cells["结束Z轴"].Value);
                }
            }
        }

        public void Save()
        {
            List<FobAoiTestPoint> data = new List<FobAoiTestPoint>();
            foreach (DataGridViewRow item in this.dgv_point.Rows)
            {
                if (item.IsNewRow)
                    continue;
                FobAoiTestPoint point = new FobAoiTestPoint();
                point.Sort = Convert.ToInt32(item.Cells["序号"].Value);
                point.Name = item.Cells["点位名称"].Value?.ToString();
                point.StartPoint_X = Convert.ToDouble(item.Cells["起始X轴"].Value ?? 0);
                point.StartPoint_Y = Convert.ToDouble(item.Cells["起始Y轴"].Value ?? 0);
                point.StartPoint_R = Convert.ToDouble(item.Cells["起始R轴"].Value ?? 0);
                point.StartPoint_Z = Convert.ToDouble(item.Cells["起始Z轴"].Value ?? 0);
                point.EndPoint_X = Convert.ToDouble(item.Cells["结束X轴"].Value ?? 0);
                point.EndPoint_Y = Convert.ToDouble(item.Cells["结束Y轴"].Value ?? 0);
                point.EndPoint_R = Convert.ToDouble(item.Cells["结束R轴"].Value ?? 0);
                point.EndPoint_Z = Convert.ToDouble(item.Cells["结束Z轴"].Value ?? 0);
                data.Add(point);
            }
            Cache.SetCache(Appbus.PartNo.CurrentPartNo + "_" + CONST.FOBAOI_TESTPOINT, JsonConvert.SerializeObject(data));
        }
    }
}
